<?php
    class maytinhdb
    {
        public function GetListInCart($masanpham = '')
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM tbl_sanpham ";
			$sql.=	"WHERE MaSanPham IN (" . $masanpham . ")";
			
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
		
		public function GetNumListInCart($masanpham = '')
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM tbl_sanpham ";
			$sql.=	"WHERE MaSanPham IN (" . $masanpham . ")";
			
			$db = new dataservice();
			return $db->ReturnNumRows($sql);
		}
        
        public function Add($TenSanPham = '', $GiaGoc = 0, $GiaKhuyenMai = 0, $SoLuong = 1, $NgayNhap = '', $HinhNho = '', $HinhTo = '', $TomTat = '', $ChiTiet = '', $MaLoai = 1)
		{
			$sql = "INSERT INTO tbl_sanpham(TenSanPham, GiaGoc, GiaKhuyenMai, SoLuong, NgayNhap, HinhNho, HinhTo, TomTat, ChiTiet, MaLoai) VALUES(" . dataservice::QuoteSmart($TenSanPham) . ", " . dataservice::QuoteSmart($GiaGoc) . ", " . dataservice::QuoteSmart($GiaKhuyenMai). ", " . dataservice::QuoteSmart($SoLuong). ", " . dataservice::QuoteSmart($NgayNhap) . ", " . dataservice::QuoteSmart($HinhNho) . ", " . dataservice::QuoteSmart($HinhTo) . ", " . dataservice::QuoteSmart($TomTat) . ", " . dataservice::QuoteSmart($ChiTiet) . ", " . dataservice::QuoteSmart($MaLoai) . ")";
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
        
        public function Update($masanpham = 1,$TenSanPham = '', $GiaGoc = 0, $GiaKhuyenMai = 0, $SoLuong = 1, $NgayNhap = '', $HinhNho = '', $HinhTo = '', $TomTat = '', $ChiTiet = '', $MaLoai = 1)
		{
			$sql  = "UPDATE tbl_sanpham ";
            $sql .= "SET TenSanPham = (" . dataservice::QuoteSmart($TenSanPham) . ") ,";
            $sql .= "GiaGoc = (" . dataservice::QuoteSmart($GiaGoc) . "), ";
            $sql .= "GiaKhuyenMai = (" . dataservice::QuoteSmart($GiaKhuyenMai) . "), ";
            $sql .= "SoLuong = (" . dataservice::QuoteSmart($SoLuong) . "), ";
            $sql .= "NgayNhap = (" . dataservice::QuoteSmart($NgayNhap) . "), ";
            $sql .= "HinhNho = (" . dataservice::QuoteSmart($HinhNho) . "), ";
            $sql .= "HinhTo = (" . dataservice::QuoteSmart($HinhTo) . "), ";
            $sql .= "TomTat = (" . dataservice::QuoteSmart($TomTat) . "), ";
            $sql .= "ChiTiet = (" . dataservice::QuoteSmart($ChiTiet) . "), ";
            $sql .= "MaLoai = (" . dataservice::QuoteSmart($MaLoai) . ") ";
			$sql .= "WHERE MaSanPham = (" . $masanpham . ")";
            
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
        
        public function Delete($masanpham = '')
        {
            $sql  = "DELETE FROM tbl_sanpham ";
            $sql .= "WHERE (1)";
            $sql .= " AND MaSanPham = (" . $masanpham . ")";
            
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
        }
        
        public function NumberEnd()
        {
            $sql = "SELECT MAX(". "MaSanPham" .") ";
            $sql .= "FROM tbl_sanpham ";
            $sql .= "WHERE (1)";
            
            $db = new dataservice();
            return $db->ExecuteQuery($sql);
        }
        
        public function GetListComputer($masanpham = 0, $tensanpham = '')
		{
			$sql =	"SELECT SP.MaSanPham, SP.TenSanPham, SP.GiaGoc, SP.GiaKhuyenMai, SP.SoLuong, SP.NgayNhap, SP.HinhNho, SP.HinhTo, SP.TomTat, SP.ChiTiet, SP.MaLoai, ML.TenLoai ";
			$sql .=	"FROM tbl_sanpham SP, tbl_loaimaytinh ML ";
			$sql .=	"WHERE (1) ";
            $sql .= "AND SP.MaLoai = ML.MaLoai";
            if($masanpham != 0)
                $sql .= " AND SP.MaSanPham IN (" . $masanpham . ")";
            
            if($tensanpham != '')
                $sql .= " AND SP.TenSanPham = " . dataservice::QuoteSmart($tensanpham);
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function GetTypeListComputer()
		{
			$sql =	"SELECT * ";
			$sql .=	"FROM tbl_loaimaytinh ";
			$sql .=	"WHERE (1) ";
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function GetListNewComputer($SoSanPham = 6)
		{
			$sql =	"SELECT * ";
			$sql .=	"FROM tbl_sanpham ";
			$sql .=	"WHERE (1) ";
            $sql .=	"ORDER BY NgayNhap DESC LIMIT 0,".$SoSanPham;
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function GetListDealsComputer($SoSanPham = 6)
		{
			$sql =	"SELECT * ";
			$sql .=	"FROM tbl_sanpham ";
			$sql .=	"WHERE (1) AND GiaKhuyenMai != GiaGoc ";
            $sql .=	"ORDER BY NgayNhap DESC LIMIT 0,".$SoSanPham;
            //echo $sql;
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function GetListBuysComputer($SoSanPham = 6)
		{            
			$sql =	"SELECT SP.MaSanPham, SP.GiaGoc, SP.GiaKhuyenMai, SP.HinhNho, SP.TenSanPham, SP.NgayNhap, CT.SoLuong ";
			$sql .=	"FROM tbl_sanpham SP ";
			$sql .=	"INNER JOIN tbl_chitietgiohang CT ON SP.MaSanPham = CT.MaSanPham ";
            $sql .=	"ORDER BY NgayNhap DESC LIMIT 0,".$SoSanPham;
            //echo $sql;
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function SearchResult($TenSanPham = '', $TomTat = '', $MaLoai = '', $giatu, $dengia, $order, $start, $num, $all)
		{
			$sql =	"SELECT S.MaSanPham, S.TenSanPham, S.GiaGoc, S.GiaKhuyenMai, S.HinhNho, S.HinhTo, S.TomTat, S.ChiTiet, S.MaLoai, L.TenLoai ";
			$sql.=	"FROM tbl_sanpham S, tbl_loaimaytinh L ";
			$sql.=	"WHERE S.MaLoai = L.MaLoai";
			
			if ($TenSanPham != '')
				$sql .= " AND (S.TenSanPham LIKE " . dataservice::QuoteSmart('%' . $TenSanPham . '%');
			
			if ($TomTat == '' && $TenSanPham != '')
				$sql .= ")";
				
			if ($TomTat != '')
				$sql .= " OR S.TomTat LIKE " . dataservice::QuoteSmart('%' . $TomTat . '%') . ")";
			
			if ($MaLoai != '')
				$sql .= " AND S.MaLoai = " . dataservice::QuoteSmart($MaLoai);
			
			if ($giatu >= 0 && $dengia > 0)
				$sql .= " AND S.GiaGoc >= " . $giatu . " AND S.GiaGoc <= " . $dengia;
			
			if ($order != '')
				$sql .= " ORDER BY " . $order . " ASC";
			
			if($all == 0)
				$sql .= " LIMIT " . $start . ", ".$num;
			
			// echo $sql;
			
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function TotalPage($sosanpham = 0, $maloai = '')
        {
            $sql  = "SELECT * FROM tbl_sanpham ";
            $sql .= "WHERE (1)";
            
            if($maloai != '')
                $sql .= " AND MaLoai = " . dataservice::QuoteSmart($maloai);
                
            $data = new dataservice();
            $tongsodong = $data->ReturnNumRows($sql);
            $tongsotrang = ceil($tongsodong / $sosanpham);
            return $tongsotrang;
        }
        
        public function Paging($sosanpham = 1, $p = 1,  $maloai = '')
        {
            $x = ($p-1) * $sosanpham;
            $sql  = "SELECT * FROM tbl_sanpham ";
            $sql .= "WHERE (1)";
            
            if($maloai != '')
                $sql .= " AND MaLoai = " . dataservice::QuoteSmart($maloai);
                
            $sql .= " limit $x,$sosanpham";
            $data = new dataservice();
            return $data->ExecuteQuery($sql);
        }
    }
?>